package com.example.myapplication;

import android.content.Intent;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;


import com.example.myapplication.adapter.PaperInfoAdapter;
import com.example.myapplication.bean.PaperInformation;
import com.example.myapplication.callback.LoginCallBack;
import com.example.myapplication.ic.PaperStudent;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class PaperInfoActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener, LoginCallBack, View.OnClickListener {

    private RecyclerView recyclerView;
    private SwipeRefreshLayout swipeRefreshLayout;
    private List<PaperInformation> list = new ArrayList<>();
    private LinearLayoutManager layoutManager;
    private PaperStudent paperStudent;
    private PaperInformation paperInformation1;
    private PaperInfoAdapter paperInfoAdapter;
    protected int lastVisibleItem = 0;
    private int TYPE_NULL = 0;
    private int TYPE_ALL = 1;
    private int TYPE_FRESH = 2;
    private int PAGE = 1;
    private int PAGE_TOTAL = 0;
    private int Type = 1;
    private String workid;
    private boolean state = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_paper_info);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent();
                intent.setClass(PaperInfoActivity.this, WorkInfoActivity.class);
                startActivity(intent);
                overridePendingTransition(android.R.anim.slide_out_right, android.R.anim.slide_in_left);
                finish();
            }
        });
        initView();
        paperInformation1 = new PaperInformation();
        paperInformation1.setType(Type);
        workid = getIntent().getExtras().getString("workid");
        paperStudent = new PaperStudent();
        paperStudent.getInfo(workid, PAGE, this);
        swipeRefreshLayout.setOnRefreshListener(this);
        paperInfoAdapter = new PaperInfoAdapter(this, list);
        recyclerView.setAdapter(paperInfoAdapter);
        layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.addOnScrollListener(onScroll);
        recyclerView.setLayoutManager(layoutManager);
        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light,
                android.R.color.holo_orange_light, android.R.color.holo_red_light);
        swipeRefreshLayout.setSize(SwipeRefreshLayout.LARGE);


    }

    RecyclerView.OnScrollListener onScroll = new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            recyclerViewOnScrollStates(newState);
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);

            lastVisibleItem = layoutManager.findLastVisibleItemPosition();

        }
    };

    private void initView() {
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
    }

    private void recyclerViewOnScrollStates(int states) {
        if (list.size() == 0) {
            Snackbar.make(swipeRefreshLayout, "还没有人提交呢！", Snackbar.LENGTH_SHORT).show();
        }
        if (states == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == paperInfoAdapter.getItemCount()) {
            if (PAGE == PAGE_TOTAL) {
                Snackbar.make(swipeRefreshLayout, "已经是最后一页了", Snackbar.LENGTH_SHORT).show();
            } else if (PAGE < PAGE_TOTAL && !state) {
                state = true;
                LoadMore();

            }
        }

    }


    public void onPreLoadMore() {
        list.add(paperInformation1);
        paperInfoAdapter.notifyDataSetChanged();
    }

    public void onPostLoadMore() {
        PAGE++;
        paperStudent.getInfo(workid, PAGE, this);


    }

    public void LoadMore() {
        onPreLoadMore();
        onPostLoadMore();
    }

    @Override
    public void onSuccess(String info) {
        Log.i("info", info);
        swipeRefreshLayout.setRefreshing(false);
        List<PaperInformation> mlist = new ArrayList<>();
        try {
            JSONObject jsonObject = new JSONObject(info);
            PAGE_TOTAL = jsonObject.getInt("page");
            JSONArray jsonArray = jsonObject.getJSONArray("info");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                JSONObject jsonObject2 = jsonObject1.getJSONObject("student_paper");
                JSONObject jsonObject3 = jsonObject1.getJSONObject("work_paper");
                String paper_id = jsonObject1.getString("paper_id");
                String paper_name = jsonObject1.getString("paper_name");
                String paper_time = jsonObject1.getString("paper_time");
                String paper_context = jsonObject1.getString("paper_context");
                String student_number = jsonObject1.getString("student_number");
                String student_name = jsonObject2.getString("student_name");
                String student_pic = jsonObject2.getString("student_pic");
                String sex = jsonObject2.getString("sex");
                String workid = jsonObject3.getString("workid");
                String workname = jsonObject3.getString("workname");
                String workinfo = jsonObject3.getString("workinfo");
                String date = jsonObject3.getString("date");
                String student_email = jsonObject2.getString("student_email");
                String student_id = jsonObject2.getString("student_id");
                PaperInformation paperInformation = new PaperInformation();
                paperInformation.setDate(date);
                paperInformation.setPaper_time(paper_time);
                paperInformation.setPaper_context(paper_context);
                paperInformation.setPaper_id(paper_id);
                paperInformation.setPaper_name(paper_name);
                paperInformation.setStudent_email(student_email);
                paperInformation.setStudent_id(student_id);
                paperInformation.setWorkname(workname);
                paperInformation.setWorkid(workid);
                paperInformation.setWorkinfo(workinfo);
                paperInformation.setStudent_number(student_number);
                paperInformation.setStudent_pic(student_pic);
                paperInformation.setStudent_name(student_name);
                paperInformation.setType(0);

                mlist.add(paperInformation);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        list.addAll(mlist);
        paperInfoAdapter.changeList(list);
        state = false;
    }

    @Override
    public void onFail(String a) {
        Snackbar.make(swipeRefreshLayout, "", Snackbar.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onRefresh() {

        swipeRefreshLayout.setRefreshing(true);
        PAGE = 1;
        list.clear();
        paperStudent.getInfo(workid, PAGE, this);

    }
}
